
<style type="text/css" media="screen">
#main {
    width: 300px;
    margin:auto;
    /*background: #ececec;*/
    padding: 10px;
    /*border: 1px solid #ccc;*/
}

#image-list {
    list-style:none;
    margin:0;
    padding:0;
}
#image-list li {
    background: #fff;
    /*border: 1px solid #ccc;*/
    text-align:center;
    padding:20px;
    margin-bottom:19px;
}
#image-list li img {
    width: 258px;
    vertical-align: middle;
    border:1px solid #474747;
}
</style>
    <div id="main">
        <div id="response">
<?php
echo $this->Form->create('Upload', array('type' => 'file', 'url' => array('controller' => 'images', 'action' => $this->action)));
echo $this->Form->input('file', array('type' => 'file', 'id'=>'images'));
echo $this->Form->input('Upload Files!', array('id'=>'btn', 'type'=>'button', 'label' => FALSE));
echo $this->Form->end();
?>
    </div>
        <ul id="image-list">

        </ul>
    </div>
    <script type="text/javascript">
        (function () {
    var input = document.getElementById("images"),
        formdata = false;

    function showUploadedItem (source) {
        var list = document.getElementById("image-list"),
            li   = document.createElement("li"),
            img  = document.createElement("img");
        img.src = source;
        li.appendChild(img);
        list.appendChild(li);
    }

    if (window.FormData) {
        formdata = new FormData();
        document.getElementById("btn").style.display = "none";
    }

    input.addEventListener("change", function (evt) {
        document.getElementById("response").innerHTML = "Uploading . . ."
        var i = 0, len = this.files.length, img, reader, file;

        for ( ; i < len; i++ ) {
            file = this.files[i];

            if (!!file.type.match(/image.*/)) {
                if ( window.FileReader ) {
                    reader = new FileReader();
                    reader.onloadend = function (e) {
                        showUploadedItem(e.target.result, file.fileName);
                    };
                    reader.readAsDataURL(file);
                }
                if (formdata) {
                    formdata.append("images[]", file);
                }
            }
        }

        if (formdata) {
            $.ajax({
                async:true,
                url: "<?php echo Router::url(array('controller'=>'images','action'=>'index')) ?>",
                type: "POST",
                data: formdata,
                processData: false,
                contentType: false,
                success: function (res) {
                    document.getElementById("response").innerHTML = res;
                }
            });
        }
    }, false);
}());
    </script>
